package com.example.shirotest.study;

import com.example.shirotest.entry.Result;
import com.example.shirotest.utils.ToolUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.concurrent.ArrayBlockingQueue;

public class Test {

    public static void main(String[] args) {
/*        Sharedata instance = new  Sharedata();
        instance.putdata();

        new Md("consumer-1",instance).start();

        new Md("consumer-2",instance).start();

        new Product("product",instance).start();*/

        //new Consumer().start();

        //LoadResource.getHtml();

        System.out.println(ToolUtil.getCurDateTimeStr());


        System.out.println("this is main");

    }

}

class validates{
    //保存已存在的链接，每个数据源最近100条数据
    private HashMap<Integer, HashSet<String>> UniqueMap = new HashMap<>();

    /**
     * 判断当前数据是否是唯一的 true-唯一  false-不是唯一
     * @param sid 数据源id
     * @param url 链接地址
     * @return
     */
    public boolean isUnique(Integer sid,String url){
        boolean bool = UniqueMap.get(sid).contains(url);
        if(bool)return false;

        //把数据加入
        HashSet<String> set = UniqueMap.get(sid);
        set.add(url);
        UniqueMap.put(sid,set);

        return true;
    }
}

class Consumer extends Thread{

    @Override
    public void run() {
        while (true) {

            String url = "http://www.baidu.coms";
            try {
                Jsoup.connect(url).get();
            } catch (IOException e) {
                //throw new RuntimeException(e);
                System.out.println("Exception : " + e.getMessage());
                continue;
            }

            System.out.println(url);

            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
               // throw new RuntimeException(e);
                System.out.println(e.getMessage());
            }

        }
    }

}

class Product extends Thread{
    private Sharedata sharedata;

    public Product(String name,Sharedata sharedata){
        super(name);
        this.sharedata = sharedata;
    }

    public void run(){
        Integer waittime = 2000;
        Integer size = 0;
        while (true){
            size = sharedata.load().size();
            if(size > 0){
                System.out.println(Thread.currentThread().getName()+ " say: Queue have " + size + " data , wait 5s");
                waittime = 5000;
            }else {
                System.out.println(Thread.currentThread().getName()+ " say: Queue is empty , push data");
                sharedata.putdata();
                waittime = 2000;
            }

            try {
                Thread.sleep(waittime);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }

}

class Md extends Thread{

    private Sharedata loadResource;

    public Md(String name,Sharedata sharedata){
        super(name);
        loadResource = sharedata;
    }

    public void run(){
        Integer waittime = 500;
        while (true){
            ArrayBlockingQueue<Rurl> load = loadResource.load();
            Rurl poll = load.poll();

            if(null==poll){
                System.out.println(Thread.currentThread().getName()+ " say: the queue is empty , wait 10s");
                waittime = 10000;

                try {
                    Thread.sleep(waittime);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }


            }else {
                System.out.println(Thread.currentThread().getName()+ " take: " + poll.getUrl());
                //LoadResource.getHtml(poll.getUrl(), poll.getRegex());
            }


        }
    }

}

class MyThread extends Thread{

    Rurl rurl;

    public MyThread(String name,Rurl rurl){
        super(name);
        this.rurl = rurl;
    }

    @Override
    public void run(){

            System.out.println(Thread.currentThread().getName() + " -- Rurl is : " + rurl.getUrl());
    }

}

class Sharedata{
    private ArrayBlockingQueue<Rurl> rurlArrayBlockingQueue = new ArrayBlockingQueue<>(10);

    public void putdata(){
        Rurl rurl01 = new Rurl("https://www.gmw.cn/",".m_left60");
        Rurl rurl02 = new Rurl("http://www.mod.gov.cn/","div.container:eq(6) .col-md-4-12 .tab-bd");
        Rurl rurl03 = new Rurl("http://www.news.cn/world/index.html",".guessLike");
        Rurl rurl04 = new Rurl("https://news.sina.com.cn/","#blk_gjxw_011");

        rurlArrayBlockingQueue.add(rurl01);
        rurlArrayBlockingQueue.add(rurl02);
        rurlArrayBlockingQueue.add(rurl03);
        rurlArrayBlockingQueue.add(rurl04);
    }

    public ArrayBlockingQueue<Rurl> load(){
        return rurlArrayBlockingQueue;
    }
}


class LoadResource{

    public static void getHtml(){

        String urllink = "https://www.jnexpert.com/";
        String regex   = "div.live-client-content div.title";

        URL urlobj = null;
        String hosturl = "";
        String hostpro = "";
        try {
            urlobj = new URL(urllink);
            hosturl = urlobj.getProtocol() + "://" + urlobj.getHost();
            hostpro = urlobj.getProtocol();
        } catch (MalformedURLException e) {
            //throw new RuntimeException(e);
            System.out.println(e.getMessage());
            return;
        }

        try {
            Document document = Jsoup.connect(urllink).get();

            Elements a = document.select(regex).select("a");

            ArrayList<Result> results = new ArrayList<>();
            for (Element element : a) {
                if(element.text().isEmpty())continue;

                //链接判断，规范化处理
                String href = element.select("a").attr("href");
                if(!href.matches("^http.*")){
                    //相对当前连接的地址
                    if(href.matches("^\\./.*")){
                        href = urllink + href.substring(2);
                    }

                    //以//为开始的地址
                    if(href.matches("^//.*")){
                        href = hostpro + ":" + href;
                    }

                    //相对域名的地址
                    if(href.matches("^/.*")){
                        href = hosturl + href;
                    }
                }

                System.out.println(href);
            }


        } catch (IOException e) {
            System.out.println(e.getMessage());
            return;
        }

    }

}


class Rurl{
    private String url;
    private String regex;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getRegex() {
        return regex;
    }

    public void setRegex(String regex) {
        this.regex = regex;
    }

    public Rurl(String url, String regex) {
        this.url = url;
        this.regex = regex;
    }
}
